#define DEBUG
#include <cstdio>
#define INF 0x7ffffff
#define MAXN 500000
#define MAXM 1000

using namespace std;

int min(int a, int b) {
  return a<b ? a : b;
}

class Network {
public:
  int n, m=1, ind[MAXM+1], to[2*MAXN+2], link[2*MAXN+2], val[2*MAXN+2], dis[MAXM+1], gap[MAXM+1];
  bool flag=false;

  void addSide(int x, int y, int z) {
    m++;
    to[m]=y;
    val[m]=z;
    link[m]=ind[x];
    ind[x]=m;
    m++;
    to[m]=x;
    val[m]=0;
    link[m]=ind[y];
    ind[y]=m;
  }

  int run(int s, int t, int have) {
    if (s==t) {
      return have;
    }
    int ret=0;
    for (int i=ind[s]; i; i=link[i]) {
      if (dis[to[i]]+1==dis[s] && val[i]) {
	int pre = run(to[i], t, min(have, val[i]));
	have-=pre, ret+=pre;
	val[i]-=pre, val[i^1]+=pre;
	if (flag || !have) {
	  break;
	}
      }
    }
    if (!(--gap[dis[s]])) {
      flag=true;
    }
    gap[++dis[s]]++;
    return ret;
  }
};

int main() {
#ifdef DEBUG
  freopen("j1990.in", "r", stdin);
  freopen("j1990.out", "w", stdout);
#endif

  static Network net;
  int n, m;
  scanf("%d %d", &n, &m);
  net.n=net.gap[0]=m;
  for (int i=1; i<=n; i++) {
    int x, y, z;
    scanf("%d %d %d", &x, &y, &z);
    net.addSide(x, y, z);
  }

  int ans=0;
  for (; !net.flag; ans+=net.run(1, m, INF));
  printf("%d", ans);

  fclose(stdin);
  fclose(stdout);
  return 0;
}
